package cn.com.homeyoda.aimate.aihealth.ui.message;

import android.os.Bundle;
import android.util.Log;
import android.view.View;

import androidx.databinding.library.baseAdapters.BR;
import androidx.lifecycle.ViewModelProviders;
import androidx.recyclerview.widget.LinearLayoutManager;

import com.chad.library.adapter.base.BaseQuickAdapter;
import cn.com.homeyoda.aimate.aihealth.R;
import cn.com.homeyoda.aimate.aihealth.app.AppViewModelFactory;
import cn.com.homeyoda.aimate.aihealth.base.BaseActivity;
import cn.com.homeyoda.aimate.aihealth.bean.ArticleBean;
import cn.com.homeyoda.aimate.aihealth.bean.MessageBean;
import cn.com.homeyoda.aimate.aihealth.bean.MessageListBean;
import cn.com.homeyoda.aimate.aihealth.bean.MessageTitleBean;
import cn.com.homeyoda.aimate.aihealth.bean.MessageTypeListBean;
import cn.com.homeyoda.aimate.aihealth.databinding.ActivityMessageBinding;
import cn.com.homeyoda.aimate.aihealth.ui.home.HomeAdapter;
import cn.com.homeyoda.aimate.aihealth.ui.home.HomeViewModel;

import java.util.ArrayList;
import java.util.List;

public class MessageActivity extends BaseActivity<ActivityMessageBinding,MessageModel>{

    private MessageTypeAdapter messageTypeAdapter;
    private MessageTypeListBean messageTypeListBean = new MessageTypeListBean();


    private MessageAdapter messageAdapter;
    private MessageListBean messageListBean = new MessageListBean();

    //private MessageBean messageBean = new MessageBean();
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

    }

    @Override
    public MessageModel initViewModel() {
        AppViewModelFactory factory = AppViewModelFactory.getInstance(getApplication());
        return ViewModelProviders.of(this, factory).get(MessageModel.class);
    }

    @Override
    public void initData() {
        super.initData();
        viewModel.messageTitleData();
        viewModel.messageData();//全部
        initRecyclerView();
    }

    private void initRecyclerView() {
        messageTypeAdapter = new MessageTypeAdapter(R.layout.message_title_item,messageTypeListBean.getData(),getApplicationContext());
        binding.setLayoutManager(new LinearLayoutManager(getApplicationContext(),LinearLayoutManager.HORIZONTAL,false));
        binding.setAdapter(messageTypeAdapter);

        messageAdapter = new MessageAdapter(R.layout.message_item,messageListBean.getRows(),getApplicationContext());
        binding.setLayoutManager2(new LinearLayoutManager(getApplicationContext()));
        binding.setAdapter2(messageAdapter);

        messageTypeAdapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                messageTypeListBean.getData().get(position).getTypeName();
                Log.i("haha",messageTypeListBean.getData().get(position).getTypeName());
                messageTypeAdapter.setPos(position);
                viewModel.messageData();
            }
        });

        messageAdapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
//                messageBean = messageBeanList.get(position);
//                Log.i("haha",messageBean.getTitle());

            }
        });

        binding.back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                finish();
            }
        });

    }

    @Override
    public void initViewObservable() {
        super.initViewObservable();
        viewModel.stateLiveData.observe(this,messageTypeListBeans -> {
            messageTypeListBean = messageTypeListBeans;
            messageTypeAdapter.setNewData(messageTypeListBean.getData());
        });

        viewModel.messageData.observe(this,messageListBeans -> {
            messageListBean = messageListBeans;
            messageAdapter.setNewData(messageListBean.getRows());
        });
    }

    @Override
    public int initContentView(Bundle savedInstanceState) {
        return R.layout.activity_message;
    }

    @Override
    public int initVariableId() {
        return BR.viewModel;
    }
}
